package com.neutech.web;

import com.neutech.dao.UserDAO;
import com.neutech.dao.impl.UserDAOimpl;
import com.neutech.entity.User;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.channels.NonReadableChannelException;
import java.util.List;

@WebServlet(name = "userServlet",urlPatterns = {"*.user"})
public class UserServlet extends HttpServlet {
    private UserDAO dao = new UserDAOimpl();
    public UserServlet(){
        System.out.println("对象已经实例化");
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //前端用户请求的拦截判断
        //根据用户请求url不同，进行不同的业务逻辑操作
        String path = request.getRequestURL().toString();
        //    System.out.println(pathUrl.substring(pathUrl.lastIndexOf("/"),pathUrl.lastIndexOf(".")));
        path = path.substring(path.lastIndexOf("/"), path.lastIndexOf("."));
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if ("/findAll".equals(path)) {
            //查询所有
            List<User> users = dao.selectAll();
            //向前台输出数据
            //转发：业务逻辑处理未完成，交给更便于编写样式，动效的jsp页面来显示数据
            //1.绑定数据
            request.setAttribute("users", users);
            //2.获取转发器
            RequestDispatcher rd = request.getRequestDispatcher("list_user.jsp");
            //3.转发
            rd.forward(request, response);

//            out.print("<table aline='center' width=;80%' cellspacing='0'cellpoadding='0'>");
//            out.print("<tr><th>ID</th><th>用户名</th><th>密码</th><th>电话</th><th>地址</th><th>注册时间</th><th>修改时间</th><th>ID当前用户状态</th></tr>");
//
//              for (User user :users){
//                 out.print("<tr>" +
//                         "<td>"+user.getId()+ "</td>" +
//                         "<td>"+user.getName()+ "</td>" +
//                         "<td>"+user.getPassword()+ "</td>" +
//                         "<td>"+user.getPhone()+ "</td>" +
//                         "<td>"+user.getAddress()+ "</td>" +
//                         "<td>"+user.getCreatTime()+ "</td>" +
//                         "<td>"+user.getUpDATEtIME()+ "</td>" +
//                         "<td>"+user.getStatus()+"</td></tr>");
//                 }

//            out.print("</table>");

        } else if ("/insert".equals(path)) {
            //从前端获取提交参数
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            String phone = request.getParameter("phone");
            String address = request.getParameter("address");
            //添加新用户
            dao.insert(new User(name, password, phone, address));

            response.sendRedirect("findAll.user");
        } else if ("/delete".equals(path)) {
            int id = Integer.parseInt(request.getParameter("id"));
            //删除
            dao.delete(id);
            response.sendRedirect("findAll.user");
//更新数据时候，先找数据
        } else if ("/load".equals(path)) {
            int id = Integer.parseInt(request.getParameter("id"));
            User user = dao.selectById(id);
            request.setAttribute("user",user);
            request.getRequestDispatcher("update_user.jsp").forward(request,response);

        } else if ("/update".equals(path)) {
            int id = Integer.parseInt(request.getParameter("id"));
            User user = dao.selectById(id);
            //user对象的属性设置
            String name = request.getParameter("name");
            user.setName(name);
            String password = request.getParameter("password");
            user.setName(password);
            String phone = request.getParameter("phone");
            user.setName(phone);
            String address = request.getParameter("address");
            user.setName(address);
            dao.update(user);
            response.sendRedirect("findAll.user");








        }else{
//            System.out.println("路径错误...");
//            response.getWriter().print("<h1>404</h1>");
            //重定向：业务逻辑处理完成之后，页面跳转的过程
            //只要么有匹配到固定的url，都会重定向到404.jsp页面
            response.sendRedirect("404.jsp");

        /*else if(){
            //根据id查找
        }else if (){
            //更新
        }*/

        }
    }

    public void findAll(HttpServletResponse response) throws IOException{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        UserDAO dao = new UserDAOimpl();
        List<User> users = dao.selectAll();
        for (User user: users){
            out.print("<p>"+ user +"</p>");
        }
    }
    public void insert(){
        UserDAO dao = new UserDAOimpl();
        dao.insert(new User("","","",""));
    }
}
